package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.user.dto.ApAuthor;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.MediaFeign;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author hsg
 * @since 2022-06-27
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {
    @Autowired
    private IApUserService apUserService;
    @Autowired
    private MediaFeign mediaFeign;
    @Autowired
    private ArticleFeign articleFeign;
    @Override
    public PageResponseResult listByStatus(AuthDto dto) {
        Integer status = dto.getStatus();
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        if(status!=null){
            queryWrapper.eq(ApUserRealname::getStatus,status);
        }
        Page<ApUserRealname> page = new Page<>(dto.getPage(),dto.getSize());
        this.page(page,queryWrapper);
        return new PageResponseResult<List<ApUserRealname>>(page.getCurrent(),(int)page.getSize(),page.getTotal(),page.getRecords());
    }

    @Override
    @GlobalTransactional
    public ResponseResult auth(AuthDto dto, int i) {
        ApUserRealname apUserRealname = this.getById(dto.getId());
        //驳回部分  0代表驳回  1代表通过
        if(i==0){

            apUserRealname.setStatus(2);
            apUserRealname.setReason(dto.getReason());
            this.updateById(apUserRealname);
        }else {

            Integer userId = apUserRealname.getUserId();
            ApUser apUser = apUserService.getById(userId);
            //通过之后，成为自媒体人，调用media，保存自媒体人数据
            WmUser wmUser = saveWmUser(apUser);
            //通过之后，作者，调用article，保存作者数据
            ApAuthor apAuthor = saveApAuthor(apUser, wmUser);
            //之前没有authorid，等author保存之后再获取出id并设置
            wmUser.setApAuthorId(apAuthor.getId());
            mediaFeign.updateWmUser(wmUser);
            //修改ApUserRealname状态
            apUserRealname.setStatus(9);
            this.updateById(apUserRealname);
            //修改apUser状态
            apUser.setIsIdentityAuthentication(true);
            apUser.setFlag(1);
            apUserService.updateById(apUser);
        }
        return ResponseResult.okResult();
    }



    private WmUser saveWmUser(ApUser apUser) {
        WmUser wmUser = new WmUser();
//        wmUser.setId();逐渐自增
        wmUser.setApUserId(apUser.getId());
        //TODO 作者id作者创建之后才有
//        wmUser.setApAuthorId();
        wmUser.setName(apUser.getName());
        wmUser.setPassword(apUser.getPassword());
        wmUser.setSalt(apUser.getSalt());
//        wmUser.setNickname();
        wmUser.setImage(apUser.getImage());
//        wmUser.setLocation();
        wmUser.setPhone(apUser.getPhone());
        wmUser.setStatus(9);
//        wmUser.setEmail();
        wmUser.setType(0);
        wmUser.setScore(0);
//        wmUser.setLoginTime();
//        wmUser.setCreatedTime(apUser.getCreatedTime());
        ResponseResult<WmUser> responseResult = mediaFeign.saveWmUser(wmUser);
        if (responseResult.getCode()==0) {
            return responseResult.getData();
        }else {
            throw new RuntimeException("远程调用异常");
        }
    }
    private ApAuthor saveApAuthor(ApUser apUser,WmUser wmUser) {
        ApAuthor apAuthor = new ApAuthor();
//        apAuthor.setId();自增
        apAuthor.setName(apUser.getName());
        apAuthor.setType(2);//0 爬取数据            1 签约合作商            2 平台自媒体人
        apAuthor.setUserId(apUser.getId());
        apAuthor.setWmUserId(wmUser.getId());
        ResponseResult<ApAuthor> responseResult = articleFeign.saveApAuthor(apAuthor);
        if (responseResult.getCode()==0) {
            return responseResult.getData();
        }else {
            throw new RuntimeException("远程调用异常");
        }
    }
}
